package com.topscore.uke.ui.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.topscore.uke.R;
import com.topscore.uke.entities.ConsumptionItemBean;
import com.topscore.uke.entities.MemberBasicInfo;
import com.topscore.uke.entities.MemberConsumpInfo;
import com.topscore.uke.entities.MemberEntity;
import com.topscore.uke.entities.NewsContent;
import com.topscore.uke.mvp.contractors.GetMemberConsumpContractor;
import com.topscore.uke.mvp.contractors.GetMemberListContractor;
import com.topscore.uke.mvp.presenters.GetMemberConsumpPresenter;
import com.topscore.uke.mvp.presenters.GetMemberCountPresenter;
import com.topscore.uke.mvp.presenters.GetMemberListPresenter;
import com.topscore.uke.ui.ToolBarCompatActivity;
import com.topscore.uke.ui.fragments.MemberListSearchResultFragment;
import com.topscore.uke.ui.loadsir_callback.EmptyCallBack;
import com.topscore.uke.ui.loadsir_callback.ErrorCallBack;
import com.topscore.uke.ui.loadsir_callback.LoadingCallBack;
import com.topscore.uke.ui.widgets.RecyclerAdapterLoadMoreView;
import com.topscore.uke.utils.DisplayImageOptionsManager;
import com.topscore.uke.utils.SnackBarUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.yokeyword.indexablerv.IndexableAdapter;
import me.yokeyword.indexablerv.IndexableLayout;

public class ConsumptionActivity extends ToolBarCompatActivity implements GetMemberConsumpContractor.View {
    private String mPhone;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
//    @Bind(R.id.swipe_refresh)
//    SwipeRefreshLayout swipeRefresh;

    private BaseQuickAdapter<MemberConsumpInfo.DataListBean, BaseViewHolder> mRecyclerAdapter;

    private GetMemberConsumpContractor.Presenter mPresenter;

    private LoadService mLoadService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_consumption);
        setCentralTitle("会员交易记录");
        mPhone = getIntent().getStringExtra("phone");
        ButterKnife.bind(this);
        mLoadService = LoadSir.getDefault().register(recyclerView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {

            }
        });
        mPresenter = new GetMemberConsumpPresenter(mPhone, this);
        initViews();
    }

    private void initViews() {
        mRecyclerAdapter = new BaseQuickAdapter<MemberConsumpInfo.DataListBean, BaseViewHolder>(R.layout.consumption_item, new ArrayList()) {
            @Override
            protected void convert(BaseViewHolder holder, MemberConsumpInfo.DataListBean item) {
                ImageView ivBrand = holder.getView(R.id.iv_brand);
             /*   ImageView ivshop = holder.getView(R.id.iv_brand);
                ImageLoader.getInstance().displayImage(item.getTitleImage(),
                        ivshop,
                        DisplayImageOptionsManager.getInstance().getDefaultOptions());*/      /*
                          holder.setText(R.id.img,item.getStoreName());*/
                holder.setText(R.id.tv_shop, item.getStoreName());
                holder.setText(R.id.tv_date, item.getOrderTime());

                holder.setText(R.id.tv_orderId, item.getStoreName());
                holder.setText(R.id.tv_shopName, item.getGoodsName());
                holder.setText(R.id.tv_shopSize, item.getGoodsSize());
                holder.setText(R.id.tv_money, item.getConsumption() / item.getConsumeNum()+"");
                holder.setText(R.id.tv_num, "X " + item.getConsumption());
//                holder.setText(R.id.tv_shopSale, item.getSales());
                holder.setText(R.id.tv_payMoney, item.getConsumption() + "");

            }
        };
        mRecyclerAdapter.setEmptyView(getLayoutInflater().inflate(R.layout.empty_view, (ViewGroup) recyclerView.getParent(),false));
        mRecyclerAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.loadNextPage();
            }
        },recyclerView);
        mRecyclerAdapter.setLoadMoreView(new RecyclerAdapterLoadMoreView());
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(mRecyclerAdapter);
        initData();
    }

    private void initData() {
        mPresenter.getMemberConsump();
//        List<ConsumptionItemBean.DataBean.DataListBean> list = new ArrayList();
//        for (int i = 0; i < 12; i++) {
//            list.add(new ConsumptionItemBean.DataBean.DataListBean());
//        }
//        mRecyclerAdapter.setNewData(list);
    }

    @Override
    public BaseQuickAdapter getRecyclerAdapter() {
        return mRecyclerAdapter;
    }

    @Override
    public void onLoading(String loadingMessage) {
        mLoadService.showCallback(LoadingCallBack.class);
    }

    @Override
    public void onLoadFinished() {
        mLoadService.showSuccess();
    }

    @Override
    public void onFail(int code, String errorMessage) {
        mLoadService.showCallback(ErrorCallBack.class);
        Toast.makeText(mContext, errorMessage, Toast.LENGTH_SHORT).show();
    }
}
